//package io.dbw.modules.quartz;
//
//
//import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
//import io.dbw.common.utils.PageUtils;
//import org.quartz.*;
//import org.quartz.DateBuilder.IntervalUnit;
//import org.quartz.impl.matchers.GroupMatcher;
//import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.scheduling.quartz.QuartzJobBean;
//import org.springframework.stereotype.Component;
//
//import java.util.*;
//
///**
// * @author pdai
// */
//@Component
//public class QuartzManager {
//
//    @Autowired
//    private Scheduler sched;
//
//    /**
//     * 创建or更新任务，存在则更新不存在创建
//     *
//     * @param jobClass     任务类
//     * @param jobName      任务名称
//     * @param jobGroupName 任务组名称
//     * @param jobCron      cron表达式
//     */
//    public void addOrUpdateJob(Class<? extends QuartzJobBean> jobClass, String jobName, String jobGroupName, String jobCron) {
//        try {
//            TriggerKey triggerKey = TriggerKey.triggerKey(jobName, jobGroupName);
//            CronTrigger trigger = (CronTrigger) sched.getTrigger(triggerKey);
//            if (trigger==null) {
//                addJob(jobClass, jobName, jobGroupName, jobCron);
//            } else {
//                if (trigger.getCronExpression().equals(jobCron)) {
//                    return;
//                }
//                updateJob(jobName, jobGroupName, jobCron);
//            }
//        } catch (SchedulerException e) {
//            e.printStackTrace();
//        }
//    }
//
//    /**
//     * 增加一个job
//     *
//     * @param jobClass     任务实现类
//     * @param jobName      任务名称
//     * @param jobGroupName 任务组名
//     * @param jobCron      cron表达式(如：0/5 * * * * ? )
//     */
//    public void addJob(Class<? extends QuartzJobBean> jobClass, String jobName, String jobGroupName, String jobCron) {
//        try {
//            JobDetail jobDetail = JobBuilder.newJob(jobClass).withIdentity(jobName, jobGroupName).build();
//            Trigger trigger = TriggerBuilder.newTrigger().withIdentity(jobName, jobGroupName)
//                    .startAt(DateBuilder.futureDate(1, IntervalUnit.SECOND))
//                    .withSchedule(CronScheduleBuilder.cronSchedule(jobCron)).startNow().build();
//
//            sched.scheduleJob(jobDetail, trigger);
//            if (!sched.isShutdown()) {
//                sched.start();
//            }
//        } catch (SchedulerException e) {
//            e.printStackTrace();
//        }
//    }
//
//    /**
//     * @param jobClass
//     * @param jobName
//     * @param jobGroupName
//     * @param jobTime
//     */
//    public void addJob(Class<? extends Job> jobClass, String jobName, String jobGroupName, int jobTime) {
//        addJob(jobClass, jobName, jobGroupName, jobTime, -1);
//    }
//
//    public void addJob(Class<? extends Job> jobClass, String jobName, String jobGroupName, int jobTime, int jobTimes) {
//        try {
//            JobDetail jobDetail = JobBuilder.newJob(jobClass).withIdentity(jobName, jobGroupName)// 任务名称和组构成任务key
//                    .build();
//            // 使用simpleTrigger规则
//            Trigger trigger;
//            if (jobTimes < 0) {
//                trigger = TriggerBuilder.newTrigger().withIdentity(jobName, jobGroupName)
//                        .withSchedule(SimpleScheduleBuilder.repeatSecondlyForever(1).withIntervalInSeconds(jobTime))
//                        .startNow().build();
//            } else {
//                trigger = TriggerBuilder
//                        .newTrigger().withIdentity(jobName, jobGroupName).withSchedule(SimpleScheduleBuilder
//                                .repeatSecondlyForever(1).withIntervalInSeconds(jobTime).withRepeatCount(jobTimes))
//                        .startNow().build();
//            }
//            sched.scheduleJob(jobDetail, trigger);
//            if (!sched.isShutdown()) {
//                sched.start();
//            }
//        } catch (SchedulerException e) {
//            e.printStackTrace();
//        }
//    }
//
//    public void updateJob(String jobName, String jobGroupName, String jobTime) {
//        try {
//            TriggerKey triggerKey = TriggerKey.triggerKey(jobName, jobGroupName);
//            CronTrigger trigger = (CronTrigger) sched.getTrigger(triggerKey);
//            trigger = trigger.getTriggerBuilder().withIdentity(triggerKey)
//                    .withSchedule(CronScheduleBuilder.cronSchedule(jobTime)).build();
//            // 重启触发器
//            sched.rescheduleJob(triggerKey, trigger);
//        } catch (SchedulerException e) {
//            e.printStackTrace();
//        }
//    }
//
//    /**
//     * 删除任务一个job
//     *
//     * @param jobName      任务名称
//     * @param jobGroupName 任务组名
//     */
//    public void deleteJob(String jobName, String jobGroupName) {
//        try {
//            sched.pauseTrigger(TriggerKey.triggerKey(jobName, jobGroupName));
//            sched.unscheduleJob(TriggerKey.triggerKey(jobName, jobGroupName));
//            sched.deleteJob(new JobKey(jobName, jobGroupName));
//        } catch (Exception e) {
//            e.printStackTrace();
//        }
//    }
//
//    /**
//     * 暂停一个job
//     *
//     * @param jobName
//     * @param jobGroupName
//     */
//    public void pauseJob(String jobName, String jobGroupName) {
//        try {
//            JobKey jobKey = JobKey.jobKey(jobName, jobGroupName);
//            sched.pauseJob(jobKey);
//        } catch (SchedulerException e) {
//            e.printStackTrace();
//        }
//    }
//
//    /**
//     * 恢复一个job
//     *
//     * @param jobName
//     * @param jobGroupName
//     */
//    public void resumeJob(String jobName, String jobGroupName) {
//        try {
//            JobKey jobKey = JobKey.jobKey(jobName, jobGroupName);
//            sched.resumeJob(jobKey);
//        } catch (SchedulerException e) {
//            e.printStackTrace();
//        }
//    }
//
//    /**
//     * 立即执行一个job
//     *
//     * @param jobName
//     * @param jobGroupName
//     */
//    public void runAJobNow(String jobName, String jobGroupName) {
//        try {
//            JobKey jobKey = JobKey.jobKey(jobName, jobGroupName);
//            sched.triggerJob(jobKey);
//        } catch (SchedulerException e) {
//            e.printStackTrace();
//        }
//    }
//
//    public PageUtils<JobDetails> queryAllJobBean(int pageNo, int pageSize) {
//        List<JobDetails> jobList = null;
//        Page page = new Page<>(pageNo, pageSize);
//        try {
//            GroupMatcher<JobKey> matcher = GroupMatcher.anyJobGroup();
//            Set<JobKey> jobKeys = sched.getJobKeys(matcher);
//            jobList = new ArrayList<>();
//            for (JobKey jobKey : jobKeys) {
//                List<? extends Trigger> triggers = sched.getTriggersOfJob(jobKey);
//                for (Trigger trigger : triggers) {
//                    JobDetails jobDetails = new JobDetails();
//                    if (trigger instanceof CronTrigger) {
//                        CronTrigger cronTrigger = (CronTrigger) trigger;
//                        jobDetails.setCronExpression(cronTrigger.getCronExpression());
//                        jobDetails.setTimeZone(cronTrigger.getTimeZone().getDisplayName());
//                    }
//                    jobDetails.setTriggerGroupName(trigger.getKey().getName());
//                    jobDetails.setTriggerName(trigger.getKey().getGroup());
//                    jobDetails.setJobGroupName(jobKey.getGroup());
//                    jobDetails.setJobName(jobKey.getName());
//                    jobDetails.setStartTime(trigger.getStartTime());
//                    jobDetails.setJobClassName(sched.getJobDetail(jobKey).getJobClass().getName());
//                    jobDetails.setNextFireTime(trigger.getNextFireTime());
//                    jobDetails.setPreviousFireTime(trigger.getPreviousFireTime());
//                    jobDetails.setStatus(sched.getTriggerState(trigger.getKey()).name());
//                    jobList.add(jobDetails);
//                }
//            }
//        } catch (SchedulerException e) {
//            e.printStackTrace();
//        }
//        page.setRecords(jobList);
//        return new PageUtils<>(page);
//    }
//
//    /**
//     * 获取所有计划中的任务列表
//     *
//     * @return
//     */
//    public List<Map<String, Object>> queryAllJob() {
//        List<Map<String, Object>> jobList = null;
//        try {
//            GroupMatcher<JobKey> matcher = GroupMatcher.anyJobGroup();
//            Set<JobKey> jobKeys = sched.getJobKeys(matcher);
//            jobList = new ArrayList<>();
//            for (JobKey jobKey : jobKeys) {
//                List<? extends Trigger> triggers = sched.getTriggersOfJob(jobKey);
//                for (Trigger trigger : triggers) {
//                    Map<String, Object> map = new HashMap<>();
//                    map.put("jobName", jobKey.getName());
//                    map.put("jobGroupName", jobKey.getGroup());
//                    map.put("description", "trigger:" + trigger.getKey());
//                    Trigger.TriggerState triggerState = sched.getTriggerState(trigger.getKey());
//                    map.put("jobStatus", triggerState.name());
//                    if (trigger instanceof CronTrigger) {
//                        CronTrigger cronTrigger = (CronTrigger) trigger;
//                        String cronExpression = cronTrigger.getCronExpression();
//                        map.put("jobTime", cronExpression);
//                    }
//                    jobList.add(map);
//                }
//            }
//        } catch (SchedulerException e) {
//            e.printStackTrace();
//        }
//        return jobList;
//    }
//
//    /**
//     * 获取所有正在运行的job
//     *
//     * @return
//     */
//    public List<Map<String, Object>> queryRunJon() {
//        List<Map<String, Object>> jobList = null;
//        try {
//            List<JobExecutionContext> executingJobs = sched.getCurrentlyExecutingJobs();
//            jobList = new ArrayList<>(executingJobs.size());
//            for (JobExecutionContext executingJob : executingJobs) {
//                Map<String, Object> map = new HashMap<>();
//                JobDetail jobDetail = executingJob.getJobDetail();
//                JobKey jobKey = jobDetail.getKey();
//                Trigger trigger = executingJob.getTrigger();
//                map.put("jobName", jobKey.getName());
//                map.put("jobGroupName", jobKey.getGroup());
//                map.put("description", "trigger:" + trigger.getKey());
//                Trigger.TriggerState triggerState = sched.getTriggerState(trigger.getKey());
//                map.put("jobStatus", triggerState.name());
//                if (trigger instanceof CronTrigger) {
//                    CronTrigger cronTrigger = (CronTrigger) trigger;
//                    String cronExpression = cronTrigger.getCronExpression();
//                    map.put("jobTime", cronExpression);
//                }
//                jobList.add(map);
//            }
//        } catch (SchedulerException e) {
//            e.printStackTrace();
//        }
//        return jobList;
//    }
//}
//
